# -*- coding: utf-8 -*-

from PalmWebsite.extensions import db
from datetime import datetime, date

#sites = db.Table("sites",
                 #db.Column("site_id", db.Integer, db.ForeignKey("site.id")),
                 #db.Column("user_id", db.Integer, db.ForeignKey("user.id"))
        #)

#class User(db.Model):
    #id = db.Column(db.Integer, primary_key=True)
    #name = db.Column(db.String(32))

    #sites = db.relationship("Site", secondary=sites,
                            #backref=db.backref("users", lazy="dynamic"))

    #def __repr__(self):
        #return "<User %s@%s>" % (self.name, self.id)


class Site(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    domain = db.Column(db.String(128), unique=True)

    ip = db.Column(db.String(15))
    pagerank = db.Column(db.SmallInteger)
    title = db.Column(db.String(128))
    keywords = db.Column(db.String(256))
    description = db.Column(db.String(512))

    beian = db.Column(db.String(32))
    organizers = db.Column(db.String(32))
    beian_date = db.Column(db.DateTime)

    date = db.Column(db.DateTime)
    last_update_date = db.Column(db.Date)

    site_ses = db.relationship("SiteSE", backref="site", lazy="dynamic")

    def __init__(self, domain, title=None, ip=None, pagerank=None, keywords=None, description=None):
        self.domain = domain
        self.ip = ip
        self.title = title
        self.keywords = keywords
        self.description = description
        self.pagerank = pagerank
        self.date = datetime.now()
        self.last_update_date = date.today()


    def update_date(self):
        self.last_update_date = datetime.now()

    def __repr__(self):
        return "<Site %r@%d>" % (self.domain, self.id)


class SiteSE(db.Model):
    """站点收录/外链"""
    id = db.Column(db.Integer, primary_key=True)

    google_index = db.Column(db.Integer)
    google_link = db.Column(db.Integer)

    baidu_index = db.Column(db.Integer)
    baidu_link = db.Column(db.Integer)

    bing_index = db.Column(db.Integer)
    bing_link = db.Column(db.Integer)

    sogou_index = db.Column(db.Integer)
    sogou_link = db.Column(db.Integer)

    so_index = db.Column(db.Integer)
    so_link = db.Column(db.Integer)

    last_update_date = db.Column(db.Date, default=date.today())

    site_id = db.Column(db.Integer, db.ForeignKey("site.id"))


    def __repr__(self):

        return "<SiteSE %d>" % (self.id)
